﻿using System;
using System.Collections.Generic;

namespace NDBGen
{
    public struct OrderedKeyedMetadataValue : IOrderedMetadata
    {
        public OrderedKeyedMetadataValue(Int32 sqlGenerationIndex, String key, String value, params Object[] args)
            : this()
        {
            this.GenerationIndex = sqlGenerationIndex;
            this.Key = key;
            this.Value = value;
            this.Args = args;
        }

        public String Value { get; private set; }
        public String Key { get; private set; }
        public Int32 GenerationIndex { get; private set; }
        public Object[] Args { get; set; }

        public OrderedKeyedMetadataValue SetArgs(params Object[] args)
        {
            this.Args = args;
            return this;
        }

        public OrderedKeyedMetadataValue AddArgs(params Object[] args)
        {
            var list = new List<Object>(this.Args);
            list.AddRange(args);
            this.Args = list.ToArray();
            return this;
        }

        public override String ToString()
        {
            if (this.Value == null) return null;
            else if (this.Args != null && this.Args.Length > 0) return String.Format(this.Value, this.Args);
            else return this.Value.ToString();
        }

        public static implicit operator String(OrderedKeyedMetadataValue v)
        {
            return v.Value;
        }

        public override Boolean Equals(Object obj)
        {
            if (obj is OrderedKeyedMetadataValue)
            {
                var that = (OrderedKeyedMetadataValue)obj;
                return this.Key == that.Key;
            }
            else return false;
        }

        public override Int32 GetHashCode()
        {
            return this.Key.GetHashCode();
        }
    }
}